package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.dao.DeptManagerDao;
import com.bed.lovewithpatientbed.dao.WorkerMessageDao;
import com.bed.lovewithpatientbed.pojo.*;
import com.bed.lovewithpatientbed.service.DeptManagerService;
import com.bed.lovewithpatientbed.service.HospitalManagerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author yg
 * @version 1.0.0
 * @ClassName HospitalService.java
 * @Description TODO
 */
@Service
@Transactional
public class DeptManagerServiceImpl implements DeptManagerService {

    @Resource
    private DeptManagerDao deptManagerDao;
    @Resource
    private WorkerMessageDao workerMessageDao;
    @Resource
    private HospitalManagerService hospitalManagerService;

    @Override
    public List<DeptManager> getAll() {
        return deptManagerDao.selectByExample(null);
    }
    @Override
    public List<DeptManager> getDeptManagerByselect(LayUiPageBean pageBean, DeptManager deptManager, Date staData , Date endDate) {

        DeptManagerExample deptManagerExample=new DeptManagerExample();
        DeptManagerExample.Criteria criteria = deptManagerExample.createCriteria();

        //根据科室对接人名字查询
        if(deptManager.getDeptManagerName()!=null&&!"".equals(deptManager.getDeptManagerName())){
            criteria.andDeptManagerNameLike("%"+deptManager.getDeptManagerName()+"%");
        }

        //根据科室订单创建时间查询
        if(staData!=null&&!"".equals(staData)){
            criteria.andDeptManagerDateGreaterThanOrEqualTo(staData);
        }
        if(endDate!=null&&!"".equals(endDate)){
            criteria.andDeptManagerDateLessThanOrEqualTo(endDate);
        }

       //设置页面
        if(pageBean.getLimit() != null
                && !"".equals(pageBean.getLimit() )
                && pageBean.getPage() != null
                && !"".equals(pageBean.getPage())){
            deptManagerExample.setOffset((pageBean.getPage() - 1) * pageBean.getLimit());
            deptManagerExample.setLimit(pageBean.getLimit().intValue());
        }

        List<DeptManager> deptManagerList = deptManagerDao.selectByExample(deptManagerExample);

        for(int i=0;i<deptManagerList.size();i++){
            deptManagerList.set(i,setClass(deptManagerList.get(i)));
        }


        return deptManagerList;
    }

    @Override
    public DeptManager selectById(Integer dmid) {
        return deptManagerDao.selectByPrimaryKey(dmid);
    }

    @Override
    public int addDeptManager(DeptManager deptManager) {
        WorkerMessage workerMessage=new WorkerMessage();
        workerMessage.setWorkMessagePhone(deptManager.getDeptManagerPhone());
        workerMessage.setWorkMessagePassword("123456");
        workerMessage.setRoleId(deptManager.getRole().getRoleId());
        workerMessage.setSoftdel(1);
        workerMessageDao.insertSelective(workerMessage);
        WorkerMessageExample example=new WorkerMessageExample();
        WorkerMessageExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(deptManager.getRole().getRoleId());
        criteria.andWorkMessagePhoneEqualTo(deptManager.getDeptManagerPhone());
        List<WorkerMessage> workerMessages = workerMessageDao.selectByExample(example);
        deptManager.setWorkerMessageId(workerMessages.get(0).getWorkerMessageId());

        deptManager.setSoftdel(1);

        return deptManagerDao.insertSelective(deptManager);
    }

    @Override
    public int delDeptManager(DeptManager deptManager) {

        //软删除
        deptManager.setSoftdel(0);
        return deptManagerDao.insert(deptManager);
    }

    @Override
    public int delbatchDeptManager(DeptManager[] deptManagers) {
        int countRow=0;
        if(deptManagers.length!=0){
            for (DeptManager deptManager:deptManagers
                 ) {
                deptManager.setSoftdel(0);
                deptManagerDao.insert(deptManager);
                countRow+=1;
            }
        }
        return countRow;
    }

    @Override
    public int updateDeptManager(DeptManager deptManager) {
        return deptManagerDao.updateByPrimaryKeySelective(deptManager);
    }

    @Override
    public Integer getCount(DeptManager deptManager, Date staData , Date endDate) {
        DeptManagerExample deptManagerExample=new DeptManagerExample();
        DeptManagerExample.Criteria criteria = deptManagerExample.createCriteria();

        //根据科室对接人名字查询
        if(deptManager.getDeptManagerName()!=null&&!"".equals(deptManager.getDeptManagerName())){
            criteria.andDeptManagerNameLike("%"+deptManager.getDeptManagerName()+"%");
        }

        //根据科室订单创建时间查询
        if(staData!=null&&!"".equals(staData)){
            criteria.andDeptManagerDateGreaterThanOrEqualTo(staData);
        }
        if(endDate!=null&&!"".equals(endDate)){
            criteria.andDeptManagerDateLessThanOrEqualTo(endDate);
        }
        Long count = deptManagerDao.countByExample(deptManagerExample);
        return count.intValue();
    }

    public DeptManager setClass(DeptManager deptManager){
        //加入员工信息
        deptManager.setWorkerMessage(workerMessageDao.selectByPrimaryKey(deptManager.getWorkerMessageId()));
        //加入医院信息
        deptManager.setHospitalManager(hospitalManagerService.selectById(deptManager.getHospitalManagerId()));

        return  deptManager;
    }


}
